package services

import (
	"github.com/labstack/gommon/log"
	"law_case_api/dto"
	"law_case_api/models"
)

// CreateCaseLog 创建案件日志信息
func (_case CaseService) CreateCaseLog(wzjCaseLog models.WzjCaseLogs) (bool, error) {
	if err := db.Create(&wzjCaseLog).Error; err != nil {
		log.Error("CreateCaseLog--创建案件日志信息失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// UpdateCaseLog 更新案件信息
func (_case CaseService) UpdateCaseLog(wzjCaseLog models.WzjCaseLogs) (bool, error) {
	if err := db.Where("id = ?", wzjCaseLog.Id).
		Select("id,case_id,log_content,log_date,log_user,log_user_id").
		Updates(&wzjCaseLog).Error; err != nil {
		log.Error("UpdateCaseLog--更新案件信息失败,失败原因：", err)
		return false, err
	}
	return true, nil
}

// GetCaseLogById 根据合同id获取合同详情
func (_case CaseService) GetCaseLogById(caseId int) (models.WzjCaseLogs, error) {
	wzjCaseLog := models.WzjCaseLogs{}
	if err := db.Where("id=?", caseId).Limit(1).Find(&wzjCaseLog).Error; err != nil {
		log.Error("GetContractById--根据合同id获取合同详情失败,失败原因：", err)
		return wzjCaseLog, err
	}
	return wzjCaseLog, nil
}

// GetCaseLogListByCondition 根据登录人信息获取合同列表
func (_case CaseService) GetCaseLogListByCondition(caseLogsListReq dto.CaseLogsListReq) ([]models.WzjCaseLogs, int64, error) {
	var wzjCaseLogs []models.WzjCaseLogs
	dataCount := int64(0)
	if err := db.Where("case_id=?", caseLogsListReq.CaseId).
		Count(&dataCount).
		Select("id,case_id,log_content,log_date,log_user,log_user_id").
		Order("log_date desc").
		Find(&wzjCaseLogs).Error; err != nil {
		log.Error("GetCaseLogListByCondition--查询失败,失败原因：", err)
		return wzjCaseLogs, dataCount, err
	}
	return wzjCaseLogs, dataCount, nil
}
